package GUILayer;

import java.awt.Color;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.List;

import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.LayoutStyle.ComponentPlacement;
import javax.swing.ListSelectionModel;
import javax.swing.border.LineBorder;
import javax.swing.border.TitledBorder;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;

import CtrlLayer.CtrlLayer;
import CtrlLayer.OrderCtrl;
import GUIadapter.PanelTableModelAdapter;
import GUIadapter.SaleOrderModel;
import ModelLayer.Employee;
import ModelLayer.SaleOrder;

@SuppressWarnings("serial")
public class PanelOrdre extends JPanel implements Updateable
{
	private List<SaleOrder> saleorderList;
	private PanelTableModelAdapter saleorderModel;
	private int indexSaleOrderModel;
	private OrderCtrl orderCtrl;
	private CtrlLayer ctrlLayer;
	private PopupOpretOrdre popupOpretOrdre;

	public PanelOrdre()
	{
		orderCtrl = new OrderCtrl();
		ctrlLayer = new CtrlLayer();
		
		popupOpretOrdre = new PopupOpretOrdre(this);
		
		JButton btnOpretSalgsordre = new JButton("Opret salgsordre");
		btnOpretSalgsordre.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0)
			{
				popupOpretOrdre.setVisible(true);
				
				createSaleOrder();
				popupOpretOrdre.addOrdernr();
				popupOpretOrdre.addEmployee();
			}
		});
		
		btnOpretSalgsordre.addActionListener(new ActionListener()
		
		{
			public void actionPerformed(ActionEvent arg0)
			{
				if ((indexSaleOrderModel >= 0) && (indexSaleOrderModel < saleorderList.size()))
				{
					update();
				}
			}
		});
		
		JButton btnOpretUdlejningsordre = new JButton("Opret udlejningsordre");
		
		JButton btnSletOrdre = new JButton("Slet ordre");
		
		JButton btnDetaljer = new JButton("Detaljer");
		
		JPanel panel = new JPanel();
		panel.setBorder(new LineBorder(new Color(0, 0, 0), 2));
		
		saleorderList = orderCtrl.getSaleOrderList();

		saleorderModel = new PanelTableModelAdapter(new SaleOrderModel(saleorderList));
		
		saleorderModel.setBorder(new TitledBorder(new LineBorder(new Color(0, 0, 0), 2), "Ordre(r)", TitledBorder.LEADING, TitledBorder.TOP, null, null));
		
		saleorderModel.getTable().setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
		
		saleorderModel.getTable().getSelectionModel().addListSelectionListener(new ListSelectionListener()
		{
			@Override
			public void valueChanged(ListSelectionEvent event)
			{
				if (event.getValueIsAdjusting() == false)
				{
					indexSaleOrderModel = saleorderModel.getTable()
						.getSelectedRow();
				}
			}
		});
		
		GroupLayout groupLayout = new GroupLayout(this);
		groupLayout.setHorizontalGroup(
			groupLayout.createParallelGroup(Alignment.TRAILING)
				.addGroup(groupLayout.createSequentialGroup()
					.addContainerGap()
					.addGroup(groupLayout.createParallelGroup(Alignment.TRAILING)
						.addComponent(saleorderModel, Alignment.LEADING, GroupLayout.DEFAULT_SIZE, 779, Short.MAX_VALUE)
						.addGroup(Alignment.LEADING, groupLayout.createSequentialGroup()
							.addComponent(btnOpretSalgsordre)
							.addGap(18)
							.addComponent(btnOpretUdlejningsordre)
							.addGap(18)
							.addComponent(btnSletOrdre)
							.addGap(18)
							.addComponent(btnDetaljer)
							.addPreferredGap(ComponentPlacement.RELATED, 81, Short.MAX_VALUE)
							.addComponent(panel, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)))
					.addContainerGap())
		);
		groupLayout.setVerticalGroup(
			groupLayout.createParallelGroup(Alignment.LEADING)
				.addGroup(groupLayout.createSequentialGroup()
					.addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
						.addGroup(groupLayout.createSequentialGroup()
							.addGap(29)
							.addGroup(groupLayout.createParallelGroup(Alignment.BASELINE)
								.addComponent(btnOpretSalgsordre)
								.addComponent(btnOpretUdlejningsordre)
								.addComponent(btnSletOrdre)
								.addComponent(btnDetaljer)))
						.addGroup(groupLayout.createSequentialGroup()
							.addContainerGap()
							.addComponent(panel, GroupLayout.PREFERRED_SIZE, 53, GroupLayout.PREFERRED_SIZE)))
					.addGap(28)
					.addComponent(saleorderModel, GroupLayout.DEFAULT_SIZE, 373, Short.MAX_VALUE)
					.addContainerGap())
		);
		
		JLabel lblDate = new JLabel("Dato: " + ctrlLayer.getDateString());
		lblDate.setFont(new Font("Tahoma", Font.PLAIN, 15));
		
		JLabel lblTime = new JLabel("Tid: " + ctrlLayer.getDateTimeString());
		lblTime.setFont(new Font("Tahoma", Font.PLAIN, 15));
		lblDate.setFont(new Font("Tahoma", Font.PLAIN, 15));
		
		GroupLayout gl_panel = new GroupLayout(panel);
		gl_panel.setHorizontalGroup(
			gl_panel.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_panel.createSequentialGroup()
					.addContainerGap()
					.addComponent(lblDate, GroupLayout.PREFERRED_SIZE, 114, GroupLayout.PREFERRED_SIZE)
					.addGap(18)
					.addComponent(lblTime, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
					.addContainerGap())
		);
		gl_panel.setVerticalGroup(
			gl_panel.createParallelGroup(Alignment.TRAILING)
				.addGroup(gl_panel.createSequentialGroup()
					.addContainerGap()
					.addGroup(gl_panel.createParallelGroup(Alignment.BASELINE)
						.addComponent(lblTime, GroupLayout.DEFAULT_SIZE, 27, Short.MAX_VALUE)
						.addComponent(lblDate, GroupLayout.DEFAULT_SIZE, 27, Short.MAX_VALUE))
					.addContainerGap())
		);
		panel.setLayout(gl_panel);
		setLayout(groupLayout);
	}
	
    public void createSaleOrder()
    {
    	int ordernr = orderCtrl.getLOrder();
    	String date = "";
    	double totalPrice = 0;
    	int deliveryDiscount = 0;
    	Employee myemployee = null;
    	String deliveryDate = "";
    	ModelLayer.Customer mycustomer = null;
    	double offer = 0;
    	orderCtrl.createSaleOrder(ordernr, date, totalPrice, myemployee, deliveryDiscount, mycustomer, deliveryDate, offer);
    	
    	update();
    }
    
    @Override
	public void update()
	{
		saleorderModel.getTable().revalidate();
		saleorderModel.getTable().repaint();
	}
}
